<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/banner.css">
</head>

<body>
    <div class="mybanner">
        <ul class="hot">
            <li>
                <a href=""> <img src="images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner4.jpg" alt=""> </a>
            </li>

        </ul>
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <ul class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"></li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
        </ul>
    </div>
    <script>
        window.onload = function() {
            var width = 730; //移动的大小
            var i = 0;

            var hot = document.querySelector('.hot');
            var mybanner = document.querySelector('.mybanner')
            var temp = hot.firstElementChild.cloneNode(true);
            hot.appendChild(temp);


            var imgChange = () => {
                i++;
                hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                hot.style.transition = '0.5s'
                if (i == hot.childElementCount - 1) { //滑动到最后一页的时候 ,要跳转到第一页(要等滑动完再跳转,跳转就是一瞬间的事情)
                    i = 0;
                    setTimeout(() => {
                        hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                        hot.style.transition = 'none';
                    }, 500)

                }
                serCircle();
            }

            //点击下一页
            var next = document.querySelector('.arrow .next');
            next.onclick = imgChange;
            //点击上一页
            var prev = document.querySelector('.arrow .prev');
            prev.onclick = () => {
                    //上一页切换效果

                    //可以先切换到最后一页再滑动到倒数第二页

                    if (i == 0) { //切换
                        i = hot.childElementCount - 1;
                        hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                        hot.style.transition = 'none'
                    }
                    i--; //直接滑动
                    setTimeout(() => {
                        hot.style.transform = 'translateX(' + (-i * width) + 'px)';
                        hot.style.transition = '0.5s';
                    }, 0)
                    serCircle();
                }
                //定时轮播
            var timer = setInterval(imgChange, 1000);
            mybanner.onmouseover = () => {
                clearInterval(timer)
            }
            mybanner.onmouseout = () => {
                timer = setInterval(imgChange, 1000)
            }



            var serCircle = () => {
                var circles = document.querySelectorAll('.circle');
                circles.forEach((item, index) => {
                    var n = item.getAttribute('data-n');
                    if (n == i) {
                        item.classList.add('active');
                    } else {
                        item.classList.remove('active')
                    }
                })
            }
            var circlelist = document.querySelector('.circle-list');
            var lis = circlelist.children;
            lis.forEach(item => item.addEventListener('click', function() {
                lis.forEach(item => item.classList.remove('active'))
                this.classList.add('active')

            }))

        }
    </script>
</body>

</html>